package com.top.ruizhong.mode.api

import com.blankj.utilcode.util.GsonUtils
import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.subscribe
import com.top.ruizhong.entity.*
import com.top.ruizhong.main.video.VideoKeepActivity
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.RxHttp


/**
 * 视频列表数据
 *
 * @param limit 条数
 * @param cateId 分类ID: -2朋友 -1推荐
 * */
fun videoIndex(
    limit: String, cateId: String, listener: SubscribeListener<VideoListResponse>
): Disposable = subscribe(
    RxHttp.get("/api/video/index")
        .add("limit", limit)
        .add("cate_id", cateId)
        .asClass(VideoListResponse::class.java)
    , listener
)

/**
 * 所有分类列表
 * */
fun videoCateList(
    listener: SubscribeListener<VideoCateListResponse>
): Disposable = subscribe(
    RxHttp.get("/api/video/cate_list")
        .asClass(VideoCateListResponse::class.java)
    , listener
)

/**
 * 默认分类列表（顶上的分类列表）
 * */
fun videoDefaultCateList(
    listener: SubscribeListener<VideoCateListResponse>
): Disposable = subscribe(
    RxHttp.get("/api/video/default_cate_list")
        .asClass(VideoCateListResponse::class.java)
    , listener
)

/**
 * 设置分类
 *
 * @param cateIds 选中的分类（逗号隔开，推荐和朋友不加入）
 * */
fun videoSetCate(
    cateIds: String, listener: SubscribeListener<VideoCateSetResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/set_cate")
        .add("cate_ids", cateIds)
        .asClass(VideoCateSetResponse::class.java)
    , listener
)

/**
 * 添加视频
 * */
fun videoAdd(
    request: VideoRequest, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    Observable.just(request).flatMap {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(request),
                Map::class.java
            ).filter { it.value != null }
        Observable.just(maps)
    }.flatMap {
        RxHttp.postForm("/api/video/add")
            .addAll(it)
            .asClass(HttpServiceData::class.java)
    }
    , listener
)


/**
 * 播放视频（播放量+1）
 *
 * @param videoId 视频id
 * */
fun videoView(
    videoId: String, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/view")
        .add("video_id", videoId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 视频点赞
 *
 * @param videoId 视频id
 * */
fun videoLike(
    videoId: String, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/like")
        .add("video_id", videoId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 视频分享数+1
 *
 * @param videoId 视频id
 * */
fun videoShares(
    videoId: String, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/shares")
        .add("video_id", videoId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 广告报名
 *
 * @param videoId 视频id
 * */
fun videoAdvertisingSignUp(
    mobile: String, videoId: String, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/advertising_sign_up")
        .add("mobile", mobile)
        .add("video_id", videoId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 编辑视频
 *
 * @param request
 * */
fun videoEdit(
    request: VideoRequest, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    Observable.just(request).flatMap {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(request),
                Map::class.java
            ).filter { it.value != null }
        Observable.just(maps)
    }.flatMap {
        RxHttp.postForm("/api/video/edit")
            .addAll(it)
            .asClass(HttpServiceData::class.java)
    }
    , listener
)

/**
 * 删除视频
 *
 * @param videoIds 视频id,多个用逗号隔开
 * */
fun videoDelete(
    videoIds: String, listener: SubscribeListener<HttpServiceData>
): Disposable = subscribe(
    RxHttp.postForm("/api/video/delete")
        .add("video_ids", videoIds)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 视频详情
 *
 * @param videoId 视频id,多个用逗号隔开
 * */
fun videoDetail(
    videoId: String, listener: SubscribeListener<VideoDetailsResponse>
): Disposable = subscribe(
    RxHttp.get("/api/video/detail")
        .add("video_id", videoId)
        .asClass(VideoDetailsResponse::class.java)
    , listener
)
